$(function () {

    ajax('http://api.douban.com/v2/movie/coming_soon?start=0&count=20');//首次页面加载时

    //切换tab页时
    var lis = $(".movieNavBar>li");
    for(var i = 0; i < lis.length; i++){
        var li = lis[i];
        $(li).on("click",function(){
            var currentUrl = $(this)[0].id;
            for(var j = 0; j < lis.length; j++){
                $(lis[j]).removeClass("active");
            }
            $(this).addClass("active");
            //每次点击按钮时
           ajax('http://api.douban.com/v2/movie/'+currentUrl+'?start=0&count=50')

        });

    }


    function ajax(url){
        $.ajax({

            type: 'get',
            //url: 'http://api.douban.com/v2/movie/'+currentUrl+'?start=0&count=20',//即将上映的电影
            //url:'http://api.douban.com/v2/movie/in_theaters?start=0&count=10',//正在上映
            //url:'http://api.douban.com/v2/movie/top250?start=0&count=10',//top250
            //url:'http://api.douban.com/v2/movie/subject/1292052',//获取某个id的详细信息
            url:url,
            dataType: 'jsonp',

            success: function (data) {
                var subjects = data.subjects;//获取所有电影数组
                var parent = $("#container");
                parent[0].innerHTML = '';
                for(var n = 0; n < subjects.length; n++){
                    var div = document.createElement("div");
                    div.className = 'grid';
                    var  imgholder = document.createElement("div");
                    imgholder.className = 'imgholder';
                    var img = document.createElement("img");
                    img.className = 'lazy';
                    var strong = document.createElement("strong");
                    strong.innerHTML = subjects[n].title;
                    var p = document.createElement("p");
                    var Maincasts = '';
                    for(var s=0; s < subjects[n].casts.length; s++){
                        if(subjects[n].casts.length >0){
                            Maincasts += subjects[n].casts[s].name + " ";
                        }
                    }

                    if(Maincasts.length >0){
                        p.innerHTML = '主演：' + Maincasts;
                    }

                    var mask = document.createElement("div");
                    mask.className = 'mask';

                    img.src = subjects[n].images.large;

                    imgholder.appendChild(img);
                    div.appendChild(imgholder);
                    div.appendChild(strong);
                    div.appendChild(p);
                    div.appendChild(mask);
                    parent[0].appendChild(div);





                }
                $("img.lazy").lazyload({
                    load: function () {
                        $('#container').BlocksIt({
                            numOfCol: 5,
                            offsetX: 8,
                            offsetY: 8
                        });
                    }
                });
                $(window).scroll(function () {
                    // 当滚动到最底部以上50像素时， 加载新内容
                    if ($(document).height() - $(this).scrollTop() - $(this).height() < 50) {
                        $('#container').append($("#test").html());
                        $('#container').BlocksIt({
                            numOfCol: 5,
                            offsetX: 8,
                            offsetY: 8
                        });
                        $("img.lazy").lazyload();
                    }
                });

                //window resize
                var currentWidth = 1100;
                $(window).resize(function () {
                    var winWidth = $(window).width();
                    var conWidth;
                    if (winWidth < 660) {
                        conWidth = 440;
                        col = 2
                    } else if (winWidth < 880) {
                        conWidth = 660;
                        col = 3
                    } else if (winWidth < 1100) {
                        conWidth = 880;
                        col = 4;
                    } else {
                        conWidth = 1100;
                        col = 5;
                    }

                    if (conWidth != currentWidth) {
                        currentWidth = conWidth;
                        $('#container').width(conWidth);
                        $('#container').BlocksIt({
                            numOfCol: col,
                            offsetX: 8,
                            offsetY: 8
                        });
                    }
                });

            },
            error: function (err) {
                console.log(err);
            }

        });
    }

    function maskSlide(obj){
        $(obj).on("mouseover",function(){
            $(this).children(".mask").stop().slideDown(500);
        })
        $(obj).on("mouseout",function(){
            $(this).children(".mask").stop().slideUp(1000);
        })
    }
    //
    //$(document).snowfall('clear');
    //$(document).snowfall({
    //    image: "../images/huaban.png",
    //    flakeCount: 30,
    //    minSize: 5,
    //    maxSize: 22
    //});
})
